/**
 * 楼盘信息
 */
define(function (require) {
    'use strict';
    var angular = require('angular');
    var util = require('util');
    var ui = require('ui');
    var _ = require('underscore');
    require('pagination');

    require('/static_public/js/angular_directive/ueditor');

    var api_prefix = G_CONFIG['api_prefix'] + '/ydxs/house-type/'; //api前缀

    angular.module('houseType',['angular.directive.ueditor'])
        .factory('houseTypeService',['$rootScope','$http',function($rootScope,$http){
            return {
                getList: function (scope) {
                    var url = api_prefix + 'get-list';
                    var data = {
                        proj_id: $rootScope.curProjectId,
                        page_index: scope.query.pageIndex,
                        page_size: scope.query.pageSize,
                        NEED_TOKEN: false
                    };
                    $http.get(url, {params:data}).then(function (res) {
                        if (res.data.retCode == 0) {
                            scope.housetypes = res.data.data.list || []; //列表
                            scope.query.total = res.data.data.total;
                        } else {
                            ui.showErrTip(res.data.errMsg);
                        }
                    }).finally(function(){
                        scope.loading = false;
                    });

                },
                addOrUpdateHousetype : function(data,scope){
                    var _this = this;
                    var url = api_prefix + 'save';
                    data.NEED_TOKEN = false;
                    $http.post(url, data).then(function (res) {
                        if (res.data.retCode == 0) {
                            _this.getList(scope);
                        } else {
                            ui.showErrTip(res.data.errMsg);
                        }
                    });
                },
                delHousetype : function(housetype,scope){
                    var _this = this;
                    var url = api_prefix + 'delete';
                    $http.post(url, {house_type_id : housetype.house_type_id,NEED_TOKEN : false}).then(function (res) {
                        if (res.data.retCode == 0) {
                            scope.query.pageIndex = util.calDeletedPage(scope.query.total,scope.query.pageIndex,scope.query.pageSize,1);
                            _this.getList(scope);
                        } else {
                            ui.showErrTip(res.data.errMsg);
                        }
                    });
                }
            };
        }])
        .controller('houseTypeCtl',['$rootScope','$scope','manageService','houseTypeService',
            function($rootScope,$scope,manageService,houseTypeService){
            $scope.loading = true;
            $scope.housetypes = [];

            $scope.query = {
                pageIndex : 1,
                pageSize : 10,
                total : 0
            };

            $scope.categorys = [{Text:'住宅',Value:'0'},{Text:'别墅',Value:'1'},{Text:'商铺',Value:'2'},{Text:'车位',Value:'3'},{Text:'写字楼',Value:'4'}];

            $scope.getCategoryByCode = function(code){
                var obj = _.find($scope.categorys ,function(obj){return obj.Value === code ;});
                if(obj){
                    return obj.Text;
                }else{
                    return '';
                }
            };

            $scope.$on('changeCurProjectId',function(event,curProjectId){
                //初始化时
                $scope.curProjectId = curProjectId;
                houseTypeService.getList($scope);
            });
            //项目为空时
            $scope.$on('noProject', function (event, data) {
                $scope.loading = false;
            });


            $scope.changedPage = function(pageIndex){
                $scope.query.pageIndex = pageIndex;
                houseTypeService.getList($scope);
            };

            $scope.showAddHosueType = function($dialogScope,$data){
                $dialogScope.categorys = $scope.categorys;
                if(!$data.category){
                    $data.category = '0';//默认住宅
                }

                require.async(['uploader'], function() {
                    $('#fileupload').fileupload({
                        dataType: 'json',
                        url: G_CONFIG['api_prefix'] + 'common/file/upload-image',
                        add:function(e,data){
                            if(util.checkIsImagFile(data.files[0])){
                                data.submit();
                                $('#img_wrap').append('<div class="upload-process">上传中...</div>');
                            }else{
                                $('#house_type_pic_err').html('请选择图片文件(jpg|jpeg|png)').show();
                            }
                        },
                        done: function(e, data) {
                            $('#img_wrap').find('.upload-process').remove();
                            var res = data.result;
                            if (res.retCode == '0') { //上传成功
                                var img_url = res.data.path;

                                $('#house_type_pic_err').html('').hide();
                                $scope.$apply(function(){
                                    if(!angular.isArray($data.pic_url_list)){
                                        $data.pic_url_list = [];
                                    }
                                    $data.pic_url_list.push(img_url);
                                });
                            } else {
                                $('#house_type_pic_err').html('上传失败:' + res.errMsg).show();
                            }
                        }
                    });
                });
            };
            $scope.addOrUpdateHousetype = function($data){
                var data = angular.copy($data);
                data.project_id = $rootScope.curProjectId;
                if(data.category === '0'){
                    data.name = data.bedroom_number+'室'+data.main_hall_number+'厅'+data.bathroom_number+'卫';
                }
                if(!data.pic_url_list || data.pic_url_list.length == 0){
                    return false;
                }else{
                    houseTypeService.addOrUpdateHousetype(data,$scope);
                }
            };

            $scope.delHousetype = function(housetype){
                if ($scope.canEdit == 0) {
                    return
                }
                util.confirm('确定删除吗？', {
                    ok: function () {
                        houseTypeService.delHousetype(housetype,$scope);
                    }
                });
            };

            $scope.$on('changeCanEdit', function (event, canEdit) {
                $scope.canEdit = canEdit;
            });

            //路由切换时
            if($rootScope.curProjectId){
                $scope.curProjectId = $rootScope.curProjectId;
                houseTypeService.getList($scope);
            }
            //项目为空时
            if($rootScope.noProject === true){
                $scope.loading = false;
            }
        }]);
});